# Copyright (C) 2006  Stephane Alnet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 

#
# For more information visit http://carrierclass.net/
#

package CCNCore::Mixin::ServerModel;
use base qw( Jifty::DBI::Record::Plugin );

use Jifty::DBI::Schema;
use Jifty::DBI::Record schema {

    # Can't use CCNCore::Mixin::System because of cross-references between 
    # CCNCore::Model::Server and ::Systems.
    column the_system =>
        references CCNCore::Model::Systems,
        is mandatory,
        is immutable,
        is indexed,
        since '0.0.1',
        label is "System",
        hints is 'The group of servers ("system") this entity belongs to.';

    # -- attributes

    column operating_system =>
        is mandatory,
        type is 'text',
        valid_values are
            { value => 'debian', display => 'Debian' },
            { value => 'unsupported', display => 'Unsupported OS' },
        since '0.0.1',
        label is "Operating System",
        hints is 'The operating system on the server.';

    column default_router =>
        is mandatory,
        # MySQL doesn't have a native "inet" type;
        # see http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_inet-aton
        # So if we wanted to do maths on IPs we could do:
        #   $system_sql =~ s/inet/INT UNSIGNED/g;
        # and insert INET functions all over the place.
        # However we don't do maths on these (except in Emergency code, which uses different tables),
        # so let's store IP addresses and masks in strings.
        type is 'VARCHAR(16)',
        validator is \&CCN::Framework::Type::IPv4::validator,
        since '0.0.1',
        label is "Default Router",
        hints is 'The default IP router for this server.';

    # -- System-wide
    column dns_master =>
        # MySQL doesn't have a real Boolean type (it's actually a tinyint), neither does SQLite3.
        # SQLite3 doesn't really care, but treats it as a string. So we'd better do the same with
        # MySQL. Note that the values are uppercase and should be uppercase in queries too (thereby
        # ressembling SQL keywords.)
        type is 'boolean',
        valid_values are
            { display => '', value => undef },
            { display => 'Yes', value => 'TRUE' },
            { display => 'No',  value => 'FALSE' },
        since '0.0.1',
        label is "System-wide DNS master",
        hints is 'Is this server a system-wide DNS master server? If it is, it should have a connection to the Public DNS servers listed for the System. You should at least have two System-wide DNS masters per System.';

    column ntp_master =>
        type is 'boolean',
        valid_values are
            { display => '', value => undef },
            { display => 'Yes', value => 'TRUE' },
            { display => 'No',  value => 'FALSE' },
        since '0.0.1',
        label is "System-wide NTP master",
        hints is 'Is this server a system-wdie NTP master server? If it is, it should have a connection to the Public NTP servers listed for the System. You should at least have two System-wide NTP masters per System';

    # -- servers used for specific services
    column cdn_upstream_server =>
        references CCNCore::Model::Server,
        is indexed,
        since '0.0.1',
        label is "Uses upstream (backup, CDR) server",
        hints is 'In each System there should be one or two servers that serve as Upstream server for the management system (which may be a separate System). Upstream servers are used to push Backup data, billing records, and monitoring information back to the management system. Indicate which of the Upstream server(s) this server should use; the CDN server you select must be a member of the same System as this server.';

    column cdn_downstream_server =>
        references CCNCore::Model::Server,
        is indexed,
        since '0.0.1',
        label is "Uses downstream (configuration, MoH) server",
        hints is 'In each System there should be one or two servers that serve as Downstream server for the management system (which may be a separate System). Downstream servers are used to push configuration information and other data from the management system to each server; they build was is traditionally called a "Content Distribution Network" (CDN). Indicate which of the Downstream server(s) this server should use; the CDN server you select must be a member of the same System as this server.';

    # --- Extensible 
    column db_member =>
        references CCNCore::Model::DatabaseCluster,
        is indexed,
        since '0.0.1',
        label is "Member of database cluser",
        hints is 'If this server is to be part of a database cluster, select the cluster.';

};

use CCNCore::Mixin::Active;
use CCNCore::Mixin::FancyName;
use CCNCore::Mixin::Owner;

1;